package com.ruoyi.web.controller.workflow;

import java.io.IOException;
import java.io.InputStream;
import java.rmi.ServerException;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.compress.utils.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workflow.domain.CarPackageAudit;
import com.ruoyi.workflow.service.ICarPackageAuditService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 套餐审核Controller
 *
 * @author lhl
 * @date 2022-11-18
 */
@RestController
@RequestMapping("/workflow/audit")
public class CarPackageAuditController extends BaseController
{
    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    /**
     * 发起套餐审核
     */
    @PreAuthorize("@ss.hasPermi('workflow:audit:start')")
    @PostMapping("/start")
    public AjaxResult startAudit(Long serviceItemId,String info) throws ServerException {
        carPackageAuditService.startAudit(serviceItemId,info);
        return AjaxResult.success("审核成功");
    }
    /**
     * 查询套餐审核列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:audit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CarPackageAudit carPackageAudit)
    {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 导出套餐审核列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:audit:export')")
    @Log(title = "套餐审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        ExcelUtil<CarPackageAudit> util = new ExcelUtil<CarPackageAudit>(CarPackageAudit.class);
        util.exportExcel(response, list, "套餐审核数据");
    }

    /**
     * 获取套餐审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('workflow:audit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(carPackageAuditService.getById(id));
    }

    /**
     * 新增套餐审核
     */
    @PreAuthorize("@ss.hasPermi('workflow:audit:add')")
    @Log(title = "套餐审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.save(carPackageAudit));
    }

    /**
     * 修改套餐审核
     */
    @PreAuthorize("@ss.hasPermi('workflow:audit:edit')")
    @Log(title = "套餐审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.updateById(carPackageAudit));
    }

    /**
     * 撤销套餐
     */
    @PreAuthorize("@ss.hasPermi('workflow:audit:remove')")
    @Log(title = "套餐审核", businessType = BusinessType.DELETE)
	@PostMapping("/revoke")
    public AjaxResult revoke(Long id,String reason) throws ServerException {
        if (!StringUtils.hasText(reason)) {
            throw new ServerException("请输入理由");
        }
        carPackageAuditService.revoke(id,reason);
        return AjaxResult.success("撤销成功");
    }
    //查询流程图
    @PreAuthorize("@ss.hasPermi('workflow:audit:resource')")
    @GetMapping("/image")
    public void getProcessingImage(String instanceId, HttpServletResponse resp) throws IOException {
        InputStream in = carPackageAuditService.getProcessingImage(instanceId);
        IOUtils.copy(in, resp.getOutputStream());
    }

}
